package com.zhuiyun.educations.api.subject.service.impl;

import com.zhuiyun.educations.api.subject.entity.Subject;
import com.zhuiyun.educations.api.subject.mapper.SubjectMapper;
import com.zhuiyun.educations.api.subject.service.SubjectService;
import com.zhuiyun.educations.common.response.CommonResult;
import com.zhuiyun.educations.util.pageUtil.MybatisPageHelper;
import com.zhuiyun.educations.util.pageUtil.PageRequest;
import com.zhuiyun.educations.util.pageUtil.PageResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName SubjectServiceImpl
 * @Description TODO 科目信息业务逻辑层实现类
 * @Author 杨友朋
 * @Date 2023/6/19 15:05
 **/
@Service
public class SubjectServiceImpl implements SubjectService {

    @Resource
    SubjectMapper subjectMapper;


    /**
     * @Author 杨友朋
     * @Description TODO 分页全查父级科目
     * @Date 2023/6/20 8:32
     * @Return PageResult
     * @param subject,pageRequest
     */
    @Override
    public PageResult selectSubject(Subject subject, PageRequest pageRequest) {
        MybatisPageHelper.startPage(pageRequest);
        List<Subject> list=subjectMapper.selectSubject(subject);
        return MybatisPageHelper.getPageResult(list);
    }

    /**
     * @Author 杨友朋
     * @Description TODO 全查父级科目
     * @Date 2023/6/21 20:26
     * @Return  List<Subject>
     * @param subject
     */
    @Override
    public List<Subject> selectAll(Subject subject) {

        return subjectMapper.selectSubject(subject);
    }

    /**
     * @Author 杨友朋
     * @Description TODO 添加父级科目
     * @Date 2023/6/21 20:28
     * @Return CommonResult
     * @param subject
     */
    @Override
    public CommonResult insertSubject(Subject subject) {
        //判断id是否存在 不存在添加
       if(null==(subject.getId())){
           Date date=new Date();
           subject.setCreateTime(date);
           subject.setUpdateTime(date);
           //持久化操作
           subjectMapper.insertSubject(subject);
           return CommonResult.ok("添加成功");
       }else {
           return CommonResult.error("添加失败");
       }
    }

    /**
     * @Author 杨友朋
     * @Description TODO 根据id删除父级科目
     * @Date 2023/6/21 20:29
     * @Return CommonResult
     * @param id
     */
    @Override
    public CommonResult deleteSubjectById(Integer id) {
        //判断id是否存在 不存在删除
        if(null!=id){
            //持久化操作
            subjectMapper.deleteSubjectById(id);
            return CommonResult.ok("删除成功");
        }else {
            return CommonResult.error("缺少参数，删除失败！");
        }
    }

    /**
     * @Author 杨友朋
     * @Description TODO 根据ids批量删除父级科目
     * @Date 2023/6/21 20:30
     * @Return CommonResult
     * @param ids
     */
    @Override
    public CommonResult deleteSubjectByIds(List<Integer> ids) {
        //判断ids是否为空 不为空删除
        if(null!=ids){
            //持久化操作
            subjectMapper.deleteSubjectByIds(ids);
            return CommonResult.ok("批量删除成功");
        }
        return CommonResult.error("缺少参数，批量删除失败！");
    }

}
